package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.ValidCardAuthorizationRes;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.ui.activity.SelectImageActivity;
import sz.szsmk.citizencard.ui.dialogs.UpLoadHeadImgDialog;
import sz.szsmk.citizencard.util.Base64;
import sz.szsmk.citizencard.util.PhotoUtils;

/**
 * 身份码首页
 */

@Route("identityCodeActivity")
public class IdentityCodeActivity extends AppBaseActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.qr_activity_identity_code);

    }


    private void addImg() {

        UpLoadHeadImgDialog upLoadHeadImgDialog = new UpLoadHeadImgDialog(IdentityCodeActivity.this);
        upLoadHeadImgDialog.setUpLoadHeadImgCallBack(new UpLoadHeadImgDialog.UPLoadHeadImgCallBack(){
            @Override
            public void callback(ImageView imageView) {
                startActivityForResult(new Intent(IdentityCodeActivity.this,
                        SelectImageActivity.class),100);

            }

            @Override
            public void upLoadImg(String path) {
                upLoad(path);
            }
        });
        upLoadHeadImgDialog.show();

    }


    @Override
    protected void initSystemBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.color_FF7466).init();
    }

    @Override
    protected CharSequence getTopTitle() {
        return "身份码";
    }

    private CountDownTimer ermCountDownTimer;
    private CountDownTimer autoRefreshCountDownTimer;

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{ };
    }

    private String qrCodeMsg;



    private String tyhmneedHead;
    private String whxfneedHead;
    private String tyhmsysLabel;
    private String whxfsysLabel;

    ValidCardAuthorizationRes.CardAuthrationInforsBean xxnkAuth;



    //以下为上传头像
    private void upLoad(String path) {
        File files = compressImage(PhotoUtils.getBitmapFromUri(Uri.fromFile(new File(path)), this));
        BitmapFactory.Options options =new BitmapFactory.Options();
        options.inJustDecodeBounds=false;
        Bitmap bitmaptemp = BitmapFactory.decodeFile(files.getAbsoluteFile().toString());
        String photo = Bitmap2StrByBase64(bitmaptemp);

    }

    public static File compressImage(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 500) {  //循环判断如果压缩后图片是否大于500kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            options -= 10;//每次都减少10
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
            long length = baos.toByteArray().length;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());
        String filename = format.format(date);
        File file = new File(Environment.getExternalStorageDirectory(), filename + "app_icon.png");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (IOException e) {
//                BAFLogger.e(TAG,e.getMessage());
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
//            BAFLogger.e(TAG,e.getMessage());
            e.printStackTrace();
        }
        recycleBitmap(bitmap);
        return file;
    }

    public String Bitmap2StrByBase64(Bitmap bit) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 40, bos);//参数100表示不压缩
        byte[] bytes = bos.toByteArray();

        //自带base64 和后台不一致
        return Base64.encode(bytes);
    }

    public static void recycleBitmap(Bitmap... bitmaps) {
        if (bitmaps == null) {
            return;
        }
        for (Bitmap bm : bitmaps) {
            if (null != bm && !bm.isRecycled()) {
                bm.recycle();
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();

        if (ermCountDownTimer != null) {
            ermCountDownTimer.cancel();
            ermCountDownTimer = null;
        }


    }
}
